#!/usr/bin/env python
#-*- coding:utf-8 -*-
'''
验证装饰器
author: cold night
email:wh_linux@126.com
'''
from model import model
from bottle import redirect
from bottle import request

def blog_auth(func):
    '''
    定义一个装饰器用于装饰需要验证的页面
    装饰器必须放在route装饰器下面
    '''
    def wrapper(*args, **kargs):
        try:
            # 读取cookie
            user = request.COOKIES.get('user', None)
            shell = request.COOKIES.get('shell', None)
        except Exception as e:
            # 出现异常则重定向到登录页面
            redirect('/login')

        # 验证用户数据
        if model.check_shell(user, shell):
            # 校验成功则返回函数
            return func(**kargs)
        else:
            # 否则则重定向到登录页面
            redirect('/login')

    return wrapper


__all__ = ['blog_auth']
